package zerous.modpeeditor;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.view.View.*;
import android.text.*;
import android.content.*;

import java.io.*;

public class MainActivity extends Activity
{
	String filename;
	EditText input,file;
	Button brace1,brace2,inden,save,itemid;
	TextView name;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

		input = (EditText)findViewById(R.id.input);
		brace1 = (Button)findViewById(R.id.brace1);
		brace2 = (Button)findViewById(R.id.brace2);
		inden = (Button)findViewById(R.id.inden);
		save = (Button)findViewById(R.id.save);
		itemid = (Button)findViewById(R.id.itemid);

		File saves=new File(Environment.getExternalStorageDirectory().getPath()+"/ModPE Scripts");
		saves.mkdir();

		inden.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					int index = input.getSelectionStart();
					String indens = "    ";
					Editable edit = input.getEditableText();
					edit.insert(index,indens);
				}
			});

		brace1.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					int index = input.getSelectionStart();
					String indens = "{}";
					Editable edit = input.getEditableText();
					edit.insert(index,indens);
					input.setSelection(input.getSelectionStart()-1);
				}
			});

		brace2.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				int index = input.getSelectionStart();
				String indens = "}";
				Editable edit = input.getEditableText();
				edit.insert(index,indens);
			}
		});

		save.setOnClickListener(new OnClickListener()
		{
				@Override
				public void onClick(View v)
				{

					AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
					file = new EditText(MainActivity.this);
					name = new TextView(MainActivity.this);
					LinearLayout layout = new LinearLayout(MainActivity.this);
					layout.addView(name);
					layout.addView(file);
					name.setText("文件名");
					builder.setView(layout);
					builder.setTitle("保存文件");
					builder.setPositiveButton("确定",new DialogInterface.OnClickListener()
						{
							@Override
							public void onClick(DialogInterface dialog,int which)
							{
								filename = file.getText().toString();
								write(filename);
								Toast.makeText(getApplicationContext(),"已保存至ModPE Scripts文件夹", Toast.LENGTH_LONG).show();

							}
						});
					builder.setNegativeButton("取消", null);
					builder.show();
				}
			});
			
		itemid.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(MainActivity.this,ItemID.class);
				startActivity(intent);
			}
		});

		/*input.setOnKeyListener(new OnKeyListener()
			{
				@Override
				public boolean onKey(View v,int keycode,KeyEvent event)
				{
					String str = "\n"+"    ";
					if(keycode == KeyEvent.KEYCODE_ENTER)
					{
						int index = input.getSelectionStart();
						Editable edit = input.getEditableText();
						edit.insert(index,str);
					}
					else
					{
						return false;
					}

					return true;
				}
			});*/
	}

	public void write(String filename)
	{
		String texts = input.getText().toString();
		try{
			File file=new File("sdcard/ModPE Scripts/"+filename+".js");
			FileWriter output=new FileWriter(file);
			output.write(texts);
			output.flush();
			output.close();
		}catch(IOException e)
		{
			
		}
	}
}
